package com.taplinker.client.im.activity.message;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;

import android.annotation.TargetApi;
import android.app.AlertDialog;
import android.content.ClipData;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.view.View;

import com.taplinker.client.AppConfigContext;
import com.taplinker.client.im.domain.ImDataCenter;
import com.taplinker.client.im.domain.ImMessageHandler;
import com.taplinker.client.im.protocol.TapLinkerMessage;
import com.taplinker.core.VersionSelecter;
/**
 * 显示信息摘要
 * @author djbian
 *
 */
public abstract class AbstractMessageView {
	
	abstract public View getMessageView(Context context, TapLinkerMessage message, View convertView);
	
	/**
	 * 获取图片路径
	 * @param msg
	 * @return
	 */
	public String getAccessUrl(final TapLinkerMessage msg) {
		String url = "";
		if(msg.getLocalUrl()!=null && msg.getLocalUrl().trim().length() > 0) {
			url = msg.getLocalUrl();
		} else if (msg.getContent()!=null && msg.getContent().trim().length()>0 ) {
			url = AppConfigContext.WEB_URL + msg.getContent();
		} else {
			url = msg.getLocalUrl();
		}
//		Log.v("log_ktian", "文件urlxxxx："+url);
		return url;
	}
	
	/**
	 * 消息长按
	 */
	final class MessageOnLongClickListener implements View.OnLongClickListener {
		private TapLinkerMessage msg;
		private Context context;
		
		public MessageOnLongClickListener(TapLinkerMessage msg, Context context) {
			this.msg = msg;
			this.context = context;
		}

		@Override
		public boolean onLongClick(View v) {
			String[] items = new String[]{"搜索", "复制"};
			if(msg.isUnarrived()) {
				items = new String[]{"重发", "搜索", "复制"};
			}
			new AlertDialog.Builder(context)
				.setTitle("我")
				.setItems(items, new DialogInterface.OnClickListener() {
					
					@Override
					public void onClick(DialogInterface dialog, int which) {
						if(!msg.isUnarrived()) {
							which++;
						}
						switch (which) {
						case 0:
							ImMessageHandler.getInstance().sendTextMessage(msg.getContent(), ImDataCenter.getInstance().getConversation());
							ImDataCenter.getInstance().getConversation().getMessages().remove(msg);
							ImDataCenter.getInstance().noticeListener(ImDataCenter.MESSAGE_FINISH);
							break;
						case 1:
							String keyword = "";
							try {
								keyword = URLEncoder.encode(msg.getContent(), "utf-8");
							} catch (UnsupportedEncodingException e) {
								e.printStackTrace();
							}
			                Uri uri = Uri.parse("http://www.google.com.hk/search?hl=zh-CN&q=" + keyword);
			                Intent intent = new Intent(Intent.ACTION_VIEW, uri);
			                context.startActivity(intent);
							break;
						case 2:
							clipboard(msg);
							break;
						default:
							break;
						}
					}
				})
				.show();
			return false;
		}
		
		
		/**
		 * 剪贴板
		 * @param msg
		 */
		@SuppressWarnings("deprecation")
		@TargetApi(Build.VERSION_CODES.HONEYCOMB)
		private void clipboard(TapLinkerMessage msg) {
			if (VersionSelecter.hasAPI11()) {
				android.content.ClipboardManager cm = (android.content.ClipboardManager) context.getSystemService(Context.CLIPBOARD_SERVICE);
				cm.setPrimaryClip(ClipData.newPlainText(msg.getContent(), msg.getContent()));
			} else {
				android.text.ClipboardManager cm = (android.text.ClipboardManager) context.getSystemService(Context.CLIPBOARD_SERVICE);
				cm.setText(msg.getContent());
			}
		}
	}
}